﻿using System;
using System.Collections.Generic;
using System.Text;
using NDde.Server;
using Microsoft.VisualBasic;
using System.Threading;


namespace BasicMachine
{
    public partial class Form1
    {
        public void StartDDEServer()
        {
            DdeServer dServer = new MyServer("RS_DDE");

            dServer.Register();
            dServer.Advise("RSTick", "Index");

            int currentIndex = 0;

                Thread.Sleep(500);
                if (Information.IsNumeric(lab_Index.Text))
                {
                    int temp = Convert.ToInt32(lab_Index.Text);
                    if (temp != currentIndex)
                    {
                        currentIndex = temp;
                        dServer.Advise("RSTick", "Index");
                    }
                }
            
        }

        private class MyServer : DdeServer
        {
            public MyServer(string service): base(service)
            {
            }

            protected override RequestResult OnRequest(DdeConversation conversation, string item, int format)
            {
                string index = ((System.Windows.Forms.Label)(Form1.ActiveForm.Controls["lab_Index"])).Text;
                if (Information.IsNumeric(index))
                    return new RequestResult(System.Text.Encoding.ASCII.GetBytes(index + "\0"));

                return RequestResult.NotProcessed;
            }

            protected override byte[] OnAdvise(string topic, string item, int format)
            {
                string index = ((System.Windows.Forms.Label)(Form1.ActiveForm.Controls["lab_Index"])).Text;
                if (Information.IsNumeric(index))
                    return System.Text.Encoding.ASCII.GetBytes(index + "\0");

                return null;
            }
        }
    }
}
